package com.atguigu.guli.service.cms.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.cms.entity.Ad;
import com.atguigu.guli.service.cms.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 广告推荐 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-11-09
 */
@RestController
@RequestMapping("/admin/cms/ad")
public class AdminAdController {
    @Autowired
    AdService adService;
    //新增广告
    @PostMapping("insertAD")
    public R insertAD(@RequestBody Ad ad){
        adService.save(ad);
        return  R.ok();
    }

    //删除广告
    @DeleteMapping("deleteById/{id}")
    public  R deleteById(@PathVariable String id){
        adService.removeById(id);
        return R.ok();
    }

    //修改广告
    @PutMapping("update")
    public R update(@RequestBody Ad ad){
        adService.updateById(ad);
        return R.ok();
    }
    //根据指定的id查询广告

    @GetMapping("findById/{id}")
    public R findById(@PathVariable String id){
        Ad ad = adService.getById(id);
        return R.ok().data("item",ad);
    }

    @Autowired
    RedisTemplate redisTemplate;

    //关于redistemplate的相关测试
    @PostMapping("save-test")
    public  R saveAd(@RequestBody Ad ad){
        //存至redis数据库中
        //存到哪个数据库，存储的结构怎么设置
        redisTemplate.opsForValue().set("ad",ad,100, TimeUnit.SECONDS);//key相同值不同，会将该key 所对应的值覆盖
        return R.ok();
    }
    @GetMapping("get-ad/{key}")
    public  R getAd(@PathVariable String key){
        Ad ad = (Ad)redisTemplate.opsForValue().get(key);
        System.out.println("ad = " + ad);
        return R.ok().data("item",ad);
    }

    @DeleteMapping("delteAd/{id}")
    public R deleteByIredis(@PathVariable String id){
        Boolean delete = redisTemplate.delete(id);
        System.out.println(delete);
        Boolean aBoolean = redisTemplate.hasKey(id);
        System.out.println(aBoolean);
        return R.ok();
    }



}

